package com.yeqiangwei.commons.image;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yeqiangwei.commons.util.TypeChange;


/**
 * 
 * for linux
 * 
 * @author aroot
 *
 */
public class ImageMagickCmd {

	private static final Log LOG = LogFactory.getLog(ImageMagickCmd.class);
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		/*ImageMagickCmd imc = new ImageMagickCmd(new File("/home/aroot/Desktop/m.jpg"));
		imc.resize(10, 10, "/home/aroot/Desktop/m2.jpg");
		imc.crop(100, 100, 100, 100, "/home/aroot/Desktop/m3.jpg");*/
	}
	
	private File imageFile;
	private String absolutePath;
	private int width;
	private int height;
	
	public ImageMagickCmd(File imageFile) {
		this.imageFile = imageFile;
		imageInfo();
	}
	
	private void imageInfo() {
		absolutePath = imageFile.getAbsolutePath();
		CmdRunner cmd = new CmdRunner("identify -format %w,%h " + imageFile.getAbsolutePath());
		try {
			String str = cmd.run();
			if(LOG.isDebugEnabled()) {
				LOG.debug("#CmdRunner# " + str);
			}
			String[] s = str.split(",");
			if(s.length < 2) {
				LOG.warn("#CmdRunner# s.length < 2, " + str);
			}
			width = TypeChange.StringToInteger(s[0], 0);
			height = TypeChange.StringToInteger(s[1], 0);
		} catch (IOException e) {
			e.printStackTrace();
			LOG.error(e.toString());
		}
	}
	
	/**
	 * 
	 * @param w
	 * @param h
	 * @param outputPath
	 * @return
	 */
	public boolean resize(int w, int h, String outputPath) {
		StringBuilder sb = new StringBuilder();
		sb.append("convert -resize ");
		sb.append(w);
		sb.append("x");
		sb.append(h);
		sb.append(" ");
		sb.append(absolutePath);
		sb.append(" ");
		sb.append(outputPath);
		CmdRunner cmdRunner = new CmdRunner(sb.toString());
		try {
			cmdRunner.run();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 
	 * @param w
	 * @param h
	 * @param x
	 * @param y
	 * @param outputPath
	 * @return
	 */
	public boolean crop(int w, int h, int x, int y, String outputPath) {
		StringBuilder sb = new StringBuilder();
		sb.append("convert -crop ");
		sb.append(w);
		sb.append("x");
		sb.append(h);
		sb.append("+");
		sb.append(x);
		sb.append("+");
		sb.append(y);
		sb.append(" ");
		sb.append(absolutePath);
		sb.append(" ");
		sb.append(outputPath);
		CmdRunner cmdRunner = new CmdRunner(sb.toString());
		try {
			cmdRunner.run();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}
	
	class CmdRunner {
		
		private String cmd;
		
		CmdRunner(String cmd) {
			this.cmd = cmd;
		}
		
		String run() throws IOException {
			System.out.println(cmd);
			Runtime rt = Runtime.getRuntime();
			Process pr = rt.exec(cmd);
			BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = input.readLine()) != null) {
				sb.append(line);
			}
			return sb.toString();
		}
	}
}
